import xlrd
import os

xlfile = "G:\\素材汇集\\模型地图\\[模型临时存放点]\\test.xls"
work_file = "G:\\素材汇集\\模型地图\\[模型临时存放点]\\test"

# 修改模型命名
data = xlrd.open_workbook(xlfile)

table = data.sheets()[0]

name_list = table.col_values(0)

for i in range(len(name_list)):
    name_list[i] = str(name_list[i])

for k in range(len(name_list)): #字符串处理
    str_number = name_list[k].rfind("\\") + 1
    if str_number != -1:
        name_list[k] = name_list[k][str_number:len(name_list[k])]

modle_number = 0

for k in range(len(name_list)):
    if "File" in name_list[k] and "mdx" in name_list[k] :
        name_list[k] = "YS" + str(modle_number).rjust(4,"0") + ".mdx"
        modle_number += 1
        
os.chdir(work_file)
files_list = []

while True:
    files = os.listdir()
    for filename in files:
        portion = os.path.splitext(filename)
        if portion[1] != '':
            r_number = int(portion[0][4:12])
            os.rename(filename,name_list[r_number + 1])
        else:
            files_list.append(os.path.abspath(filename))

    if len(files_list) > 0:
        os.chdir(files_list.pop())
    else:
        print("")
        break
